<!--
 * @Author       : cxh
 * @Date         : 2021-12-21 20:19:22
 * @LastEditTime : 2021-12-21 20:50:44
 * @LastEditors  : cxh
 * @Description  : 
 * @FilePath     : \vue_WorkSpace\day02\computed.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.common.dev.js"></script>

<body>
    <div id="example">
        <p>Original message: "{{ message }}"</p>
        <p>Computed reversed message: "{{ reversedMessage }}"</p>
        <p>Computed reversed message2: "{{ reversedMessages() }}"</p>
        <p>Computed reversed message2: "{{ now }}"</p>


    </div>

    <div id="demo">{{ fullName }}</div>
</body>
<script>
    var vm = new Vue({
        el: "#example",
        data: {
            message: 'hello'
        },
        computed: {
            reversedMessage: function () {
                return this.message.split('').reverse().join("")
            },
            now: function () {
                return Date.now()
            }
        },
        methods: {
            reversedMessages: function () {
                return this.message.split('').reverse().join('')
            }
        }
    })


    var vm1 = new Vue({
        el: '#demo',
        data: {
            firstName: 'Foo1',
            lastName: 'Bar',
            // fullName: 'Foo Bar'
        },
        computed: {
            fullName: {
                // getter
                get: function () {
                    return this.firstName + ' ' + this.lastName
                },
                // setter
                set: function (newValue) {
                    var names = newValue.split(' ')
                    this.firstName = names[0]
                    this.lastName = names[names.length - 1]
                }
            }
        }
    })
</script>

</html>